package com.warpcode.gpsloggerlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EditActivity extends Activity 
{
	private final static int RESULT_FAILED = 2;     
	LocationDatabase db;       
	String id;
	
	EditText lat,lon,alt,spe;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit);              
        
		Intent intent = getIntent();
                
		db = new LocationDatabase(this);
                
		TextView tim = (TextView) findViewById(R.id.timetext);
	    
		lat = (EditText) findViewById(R.id.latitudeedit);
		lon = (EditText) findViewById(R.id.longitudeedit);
		alt = (EditText) findViewById(R.id.altitudeedit);
		spe = (EditText) findViewById(R.id.speededit);
                
		id = intent.getStringExtra("id");
		tim.setText("Time : "+intent.getStringExtra("time"));
		lat.setText(intent.getStringExtra("latitude"));
		lon.setText(intent.getStringExtra("longitude"));
		alt.setText(intent.getStringExtra("altitude"));
		spe.setText(intent.getStringExtra("speed"));
	}
        
	        
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
			case android.R.id.home:
				cancel(null);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
        
	public void saveChanges(View view)
	{        
		String vals[] = {lat.getText().toString(),lon.getText().toString(),alt.getText().toString(),spe.getText().toString()};
                
		Intent intent = getIntent();
		intent.putExtra("latitude",lat.getText().toString());
		intent.putExtra("longitude",lon.getText().toString());
		intent.putExtra("altitude",alt.getText().toString());
		intent.putExtra("speed",spe.getText().toString());
                
		if(db.editWithId(Integer.parseInt(id), vals))
			this.setResult(RESULT_OK, intent);
		else
			this.setResult(RESULT_FAILED);  
                
		finish();
	}
        
	public void cancel(View view)
	{
		this.setResult(RESULT_CANCELED);
		finish();
	}
	
}